//
//  ViewController.m
//  HomeWork-19
//
//  Created by 鹏飞罗 on 16/8/9.
//  Copyright © 2016年 罗鹏飞. All rights reserved.
//

#import "ViewController.h"
#import "CarTableViewCell.h"
#import "Car.h"
#define kTableViewCell @"reuse"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *allCarArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableView *headTableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _allCarArray = [NSMutableArray array];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
//    [_tableView registerClass:[CarTableViewCell class] forCellReuseIdentifier:kTableViewCell];
    
    UINib *nib = [UINib nibWithNibName:@"CarTableViewCell" bundle:[NSBundle mainBundle]];
    [_tableView registerNib:nib forCellReuseIdentifier:kTableViewCell];
    
    _headTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 200) style:UITableViewStyleGrouped];
    self.tableView.tableHeaderView = _headTableView;
    
    
}

- (void)handleData {
    NSString *carPath = [[NSBundle mainBundle] pathForResource:@"AllCar" ofType:@"json"];
    NSData *carData = [NSData dataWithContentsOfFile:carPath];
    NSError *error = nil;
  NSDictionary *carDic = [NSJSONSerialization JSONObjectWithData:carData options:NSJSONReadingMutableContainers error:&error];
    _allCarArray = carDic[@"result"] [@"brandlist"];

}
#pragma mark - 头试图


#pragma mark - UITableViewDelegate
// row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView class] == _tableView) {
    CGFloat height = self.view.frame.size.height / 10;
    return height;
    } else {
        return 0;
    }
    
}
//索引栏
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    if ([tableView class] == _tableView) {
    NSMutableArray *brandArray = [NSMutableArray array];
    for (int i = 0; i < _allCarArray.count; i++) {
        NSDictionary *brand = _allCarArray[i];
        NSString *indexTitle = brand[@"letter"];
        [brandArray addObject:indexTitle];
    }
    return brandArray;
    } else {
    return nil;
    }
}
// section 的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([tableView class] == _tableView) {
    return _allCarArray.count;
    } else {
    return 0;
    }
}
//section的标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if ([tableView class] == _tableView) {
        NSDictionary *brand = _allCarArray[section];
        return brand[@"letter"];
    } else {
        
    
    return nil;
    }
}
#pragma mark - UITableDataSource
// 一个section的row个数
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableView class] == _tableView) {
    NSDictionary *brandlist = _allCarArray[section];
    NSArray *list = brandlist[@"list"];
    return list.count;
    } else {
        
    
    return 10;
    }
}
// 每个cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView class] == _tableView) {
        CarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCell];
        NSDictionary *brand = _allCarArray[indexPath.section];
        NSArray *carList = brand[@"list"];
        NSDictionary *carDic = carList[indexPath.row];
        Car *car = [Car carWithDictionary:carDic];
        //    cell.car = car;
        [cell setCar:car];
        return cell;
    } else {
    return nil;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
